<?php

namespace App\Http\Controllers\Api;

use App\Model\TrendsTopic;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class TopicController extends BaseController
{
    public function __construct(Request $request, TrendsTopic $trendsTopic)
    {
        parent::__construct($request);
        $this->model = $trendsTopic;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return $this->toJson(self::SUCCESS, $this->model->getList($request));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = [
            'user_id' => $request->input('user_id'),
            'topic_name' => $request->input('topic_name'),
            'topic_desc' => $request->input('topic_desc'),
            'topic_image' => $request->input('topic_image'),
            'topic_type' => $request->input('topic_type'),
            'topic_state' => $request->input('topic_state'),
            'sort' => $request->input('sort')
        ];
        $res = $this->model->createOrUpdateData($data);
        if($res['code'] === 0) {
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR, [], $res['msg']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = [
            'topic_id' => $id,
            'user_id' => $request->input('user_id'),
            'topic_name' => $request->input('topic_name'),
            'topic_desc' => $request->input('topic_desc'),
            'topic_image' => $request->input('topic_image'),
            'topic_type' => $request->input('topic_type'),
            'topic_state' => $request->input('topic_state'),
            'sort' => $request->input('sort'),
            'url' => $request->input('url')
        ];
    
        $res = $this->model->createOrUpdateData($data);
        if($res['code'] === 0) {
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR,[], $res['msg']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return $this->toJson(self::SUCCESS, $this->model->deleteData($id));
    }
}
